﻿function next(elem) {
    do {
        elem = elem.nextSibling;
    } while (elem && elem.nodeType != 1);
    return elem;
}
//---查找第一个子元素的函数---//    
function first(elem) {
    elem = elem.firstChild;
    return elem && elem.nodeType != 1 ? next(elem) : elem;
}

var Marquee = {
    init: function (id, toFollow, speed) {
        this.speed = speed || 10;
        this.boxID = id;
        this.toFollow = toFollow;
        this.scrollBox = document.getElementById(id);


        if (this.toFollow == "top" || this.toFollow == "bottom") {
            this.appendBox = first(this.scrollBox).cloneNode(true);
            this.scrollBox.appendChild(this.appendBox);
        } else {
            var templateLeft = "<table cellspacing='0' cellpadding='0' style='border-collapse:collapse;display:inline;'><tr><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>" + this.scrollBox.innerHTML + "</td><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>" + this.scrollBox.innerHTML + "</td></tr></table>";
            this.scrollBox.innerHTML = templateLeft;
            this.appendBox = first(first(first(first(this.scrollBox))));

        }
        this.objs = {
            scrollBox: this.scrollBox,
            appendBox: this.appendBox,
            toFollow: this.toFollow,
            speed: this.speed,
            id: this.boxID
        };
        return this;
    },

    scrollUp: function () {
        var self = this.objs;
        self.begin = function () {

            if (self['toFollow'] == "top") {
                self.doScr = setInterval(function () {
                    if (self['appendBox'].offsetHeight <= self['scrollBox'].scrollTop) {
                        self['scrollBox'].scrollTop -= first(self['scrollBox']).offsetHeight;
                    } else {
                        self['scrollBox'].scrollTop++;
                    }
                }, self.speed);
            } else if (self['toFollow'] == "bottom") {
                self.doScr = setInterval(function () {
                    if (self['scrollBox'].scrollTop <= 0) {
                        self['scrollBox'].scrollTop = self['appendBox'].offsetHeight;
                    } else {
                        self['scrollBox'].scrollTop--;
                    }
                }, self.speed);
            } else if (self['toFollow'] == "left") {
                self.doScr = setInterval(function () {
                    if (self['appendBox'].offsetWidth <= self['scrollBox'].scrollLeft) {
                        self['scrollBox'].scrollLeft -= self['appendBox'].offsetWidth;
                        
                    } else {
                        self['scrollBox'].scrollLeft++;
                        
                    }
                }, self.speed);
            } else if (self['toFollow'] == "right") {
                self.doScr = setInterval(function () {
                    if (self['scrollBox'].scrollLeft <= 0) {
                        self['scrollBox'].scrollLeft = self['appendBox'].offsetWidth;
                    } else {
                        self['scrollBox'].scrollLeft--;
                    }

                }, self.speed);

            }
        }
        self.begin();
        self.scrollBox.onmouseover = function () {
            clearInterval(self.doScr);
        }

        self.scrollBox.onmouseout = function () {
            self.begin();
        }
    }

}